package ru.dc.gui.swing.util;

import javax.swing.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 01.11.2010
 * Time: 14:27:23
 * </pre>
 */
public class IconLoader {

    private static IconLoader instance;
    private static final String IMAGE_PATH = "icons/";
    private Map<String, Icon> iconMap = new HashMap<String, Icon>();

    public ImageIcon getIcon(String name) {
        if ((name == null) || (name.length() == 0)) return null;

        ImageIcon result = (ImageIcon) iconMap.get(name);
        if (result != null) return result;

        ClassLoader cl = this.getClass().getClassLoader();
        if (cl == null) return null;
        URL url = cl.getResource(IMAGE_PATH + name);

        if (url != null) {
            result = new ImageIcon(url);
            iconMap.put(name, result);
        }
        return result;
    }

    public static IconLoader getInstance() {
        if (instance==null) {
            instance = new IconLoader();
        }
        return instance;
    }
}
